package com.atguigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.acl.mapper.PermissionMapper;
import com.atguigu.ssyx.acl.service.PermissionService;
import com.atguigu.ssyx.acl.service.RolePermissionService;
import com.atguigu.ssyx.acl.utils.PermissionHelper;
import com.atguigu.ssyx.model.acl.Permission;
import com.atguigu.ssyx.model.acl.RolePermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @作者:胡树苑
 * @时间:2025/9/11 14:46
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private RolePermissionService rolePermissionService;
    @Override
    public List<Permission> queryAllPermission() {
        //1.查询所有菜单
        List<Permission> allPermissions = baseMapper.selectList(null);
        //转换成要求数据格式
        return PermissionHelper.buildPermission(allPermissions);
    }

    @Override
    public void removeChildById(Long id) {
        //1.创建list集合idlist,封装所有要删除的菜单id
        List<Long> idList = new ArrayList<>();
        //根据当前菜单id,获取当前菜单下面所有子菜单
        //如果子菜单下面还有子菜单，都要得到
        //重点:递归找当前菜单子菜单
        this.getAllPermissionId(id,idList);
        //设置当前菜单id
        idList.add(id);
        //调用方法根据多个菜单id删除
        baseMapper.deleteBatchIds(idList);
    }

    /**
     * 查询角色及所属菜单
     * @param roleId 角色id
     * @return
     */
    @Override
    public List<Permission> getPermissionByRoleId(Long roleId) {
        //查询所有菜单
        List<Permission> permissions = baseMapper.selectList(null);
        //根绝角色id查询菜单id
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePermission::getRoleId,roleId);
        List<RolePermission> rolePermissionList = rolePermissionService.list(wrapper);
        //获取到菜单id
        List<Long> permissionIdList = rolePermissionList.stream().map(c -> c.getPermissionId()).collect(Collectors.toList());
        permissions.stream().forEach(c->{
            if (permissionIdList.contains(c.getId())){
                c.setSelect(true);
            }else {
                c.setSelect(false);
            }
        });
        //封装为树形结构
        List<Permission> permissionList = PermissionHelper.buildPermission(permissions);
        return permissionList;
    }

    /**
     * 为角色分配菜单
     * @param roleId 角色id
     * @param permissionId 菜单id集合
     */
    @Override
    public void doAssign(Long roleId, Long[] permissionId) {
        //根据角色id删除菜单角色表里数据
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePermission::getRoleId,roleId);
        rolePermissionService.remove(wrapper);
        ArrayList<RolePermission> list = new ArrayList<>();
        //获取角色分配的菜单id，并存入
        for (Long id : permissionId) {
            if (id == null){
                continue;
            }
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(id);
            list.add(rolePermission);
        }
        //添加到表中
        rolePermissionService.saveBatch(list);
    }


    //重点:递归找当前菜单子菜单
    //第一个参数当前菜单id
    //第二个参数最终封装的list集合,包含所有菜单id
    private void getAllPermissionId(Long id, List<Long> idList) {
        //根据当前id查询
        LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Permission::getPid, id);
        List<Permission> childList = baseMapper.selectList(queryWrapper);
        //递归查询是否还有子菜单
        childList.stream().forEach(item -> {
            idList.add(item.getId());
            this.getAllPermissionId(item.getId(), idList);
        });
    }
}
